<?php

/* * ************************************************************************************************
  |
  | AudioShare - Audio Sharing Script
  | http://www.audiosharescript.com
  | webmaster@audiosharescript.com
  |
  |**************************************************************************************************
  |
  | By using this software you agree that you have read and acknowledged our End-User License
  | Agreement available at http://www.audiosharescript.com/eula.html and to be bound by it.
  |
  | Copyright (c) 2010 AudioShareScript.com. All rights reserved.
  |************************************************************************************************* */

include("include/config.php");
include("include/functions/import.php");
$thebaseurl = $config['baseurl'];

$isvideouploadpage = "1";
STemplate::assign('isvideouploadpage', $isvideouploadpage);

$mainmenu = "5";
STemplate::assign('mainmenu', $mainmenu);

$submenu = "11";
STemplate::assign('submenu', $submenu);

$countriesList = listcountries("");
STemplate::assign('countriesList', $countriesList);


extract($_REQUEST);
/*if ($upload_id) {
        $data = uploadprogress_get_info($upload_id);

    if (!$data)
        $data['error'] = $lang['147'];
    else {
        $avg_kb = $data['speed_average'] / 1024;
        if ($avg_kb < 100)
            $avg_kb = round($avg_kb, 1);
        else if ($avg_kb < 10)
            $avg_kb = round($avg_kb, 2);
        else
            $avg_kb = round($avg_kb);
        $data['kb_average'] = $avg_kb;
        $data['kb_uploaded'] = round($data['bytes_uploaded'] / 1024);
    }
    echo json_encode($data);
    exit;
} */


function genUploadKey($length = 11) {
    $charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for ($i = 0; $i < $length; $i++)
        $key .= $charset[(mt_rand(0, (strlen($charset) - 1)))];
    return $key;
}

if ($_SESSION[USERID] != "" && $_SESSION[USERID] >= 0 && is_numeric($_SESSION[USERID])) {
    if ($config['verify_upload'] != "0") {
        if ($_SESSION[VERIFIED] != "1") {
            $error2 = "1";
        }
    }

    $upload_id = genUploadKey();
    STemplate::assign('upload_id', $upload_id);

    if ($_REQUEST['UPLOAD_IDENTIFIER'] != "") {
        $atitle = cleanit($_REQUEST['title']);
        $adescription = cleanit($_REQUEST['vwdescription']);
        $tags = cleanit($_REQUEST['tags']);
        $artist = cleanit($_REQUEST['artist']);
        $category = intval(cleanit($_REQUEST['category']));
        $thepublic = cleanit($_REQUEST['public']);
        $upload_file = $_FILES['audio_file']['name'];
        $pos = strrpos($upload_file, ".");
        $ph = strtolower(substr($upload_file, $pos + 1, strlen($upload_file) - $pos));
        $theallowcomments = cleanit($_REQUEST['allowcomments']);
        $theallowratings = cleanit($_REQUEST['allowratings']);
        $theallowembeds = cleanit($_REQUEST['allowembeds']);
        $theallowdownloads = cleanit($_REQUEST['allowdownloads']);
        $country = cleanit($_REQUEST["artist_country"]);
        $curyear=date("Y");
        $catName=fetch_category_name_from_id($category);
        
        STemplate::assign('title', $atitle);
        STemplate::assign('desc', $adescription);
        STemplate::assign('tags', $tags);
        STemplate::assign('category', $category);
        STemplate::assign('privacy', $thepublic);

        if ($atitle == "") {
            $error = $lang['854'];
        } elseif ($adescription == "") {
            $error = $lang['856'];
        } elseif ($tags == "") {
            $error = $lang['855'];
        } elseif ($category == "0") {
            $error = $lang['462'];
        } elseif ($ph != "mp3" && $ph != "MP3") {
            $error = $lang['148'] . " " . $lang['149'];
        } else {
            if ($thepublic == "") {
                $thepublic = "1";
            }
            if ($video_privacy == "private") {
                $thepublic = "0";
            } else {
                $thepublic = "1";
            }

            $approve_audios = $config[approve_audios];
            if ($approve_audios == 1) {
                $active = "active='0'";
            } else {
                $active = "active='1'";
            }

            $query = "INSERT INTO audios SET USERID='" . mysql_real_escape_string($_SESSION[USERID]) . "', title='" . mysql_real_escape_string($atitle) . "', description='" . mysql_real_escape_string($adescription) . "', tags='" . mysql_real_escape_string($tags) . "',artist='" . mysql_real_escape_string($artist) . "',country='" . mysql_real_escape_string($country) . "', categories='" . mysql_real_escape_string($category) . "' $addsql, public='" . mysql_real_escape_string($thepublic) . "', time_added='" . time() . "', date_added='" . date("Y-m-d") . "', $active, allowcomments='" . mysql_real_escape_string($theallowcomments) . "', allowratings='" . mysql_real_escape_string($theallowratings) . "', allowembeds='" . mysql_real_escape_string($theallowembeds) . "', allowdownloads='" . mysql_real_escape_string($theallowdownloads) . "'";
            $conn->execute($query);
            $audioid = mysql_insert_id();
            
            
            if(!is_dir($config['basedir'] . "/adata/".$catName))
             mkdir($config['basedir'] . "/adata/".$catName);
            
            if(!is_dir($config[basedir] . "/adata/".$catName."/".$curyear))
                mkdir($config['basedir'] . "/adata/".$catName."/".$curyear);
          
           /* if(!is_dir($config['audiodir'] . "/".$curyear))
                 mkdir($config['audiodir'] . "/".$curyear);
            */
            
            $original = $config['basedir'] . "/adata/".$catName."/".$curyear. "/" . $audioid . "." . $ph;
            $uploaded = $_FILES['audio_file']['tmp_name'];
            move_uploaded_file($uploaded, $original);

            if (file_exists($original)) {
                $oname = $audioid . "." . $ph;
                $upload_file_size = filesize($original);
                $space = round($upload_file_size / (1024 * 1024));

                exec("mplayer -vo null -ao null -frames 0 -identify $original", $difh);
                while (list($k, $v) = each($difh)) {
                    if ($length = strstr($v, 'ID_LENGTH='))
                        break;
                }
                $lx = explode("=", $length);
                $runtime = $lx[1];

                $query = "UPDATE audios SET original_audio='$oname', audio_name='" . $audioid . "." . $ph . "', extension='." . $ph . "', runtime='$runtime', filesize='$space' WHERE AUDIOID='$audioid'";
                $conn->execute($query);

                /*$athumb = $config['athumbdir'] . "/default.png";
                copy($athumb, $config['athumbdir'] . "/" . $audioid . "-1.png");
                $athumb = $config['athumbdir'] . "/default2.png";
                copy($athumb, $config['athumbdir'] . "/" . $audioid . "-2.png");*/

                STemplate::assign('title', "");
                STemplate::assign('desc', "");
                STemplate::assign('tags', "");
                STemplate::assign('category', "");
                STemplate::assign('privacy', "");

                $uploadedimage = $_FILES['audiopictureimage']['tmp_name'];
                
                if ($uploadedimage != "") {
                    
                    $theimageinfo = getimagesize($uploadedimage);
                    $thepp = $audioid . "";

                    if ($theimageinfo[2] == 1) {
                        $thepp .= ".gif";
                        $ext = ".gif";
                    } elseif ($theimageinfo[2] == 2) {
                        $thepp .= ".jpg";
                        $ext = ".jpg";
                    } elseif ($theimageinfo[2] == 3) {
                        $thepp .= ".png";
                        $ext = ".png";
                    } else {
                        $error = "$lang[553]";
                    }

                    if ($error == "") {
                        $myvideoimgnew = $config['athumbdir'] . "/" . $thepp;
                        if (file_exists($myvideoimgnew)) {
                            unlink($myvideoimgnew);
                        }
                        $myconvertimg = $_FILES['audiopictureimage']['tmp_name'];

                        move_uploaded_file($myconvertimg, $myvideoimgnew);

                        $max_width_thumbs = $config['max_thumb_width'];
                        $max_height_thumbs = $config['max_thumb_height'];

                        $tothumbdir = $config['athumbdir'] . "/" . $audioid . "-1.jpg";
                        if (file_exists($tothumbdir)) {
                            unlink($tothumbdir);
                        }
                        generatevideothumbs($myvideoimgnew, $tothumbdir, $max_width_thumbs, $max_height_thumbs);

                        $tothumbdir2 = $config['athumbdir'] . "/" . $audioid . "-2.jpg";
                        if (file_exists($tothumbdir2)) {
                            unlink($tothumbdir2);
                        }
                        generatevideothumbs2($myvideoimgnew, $tothumbdir2, "144", "144");

                        if (file_exists($myvideoimgnew)) {
                            unlink($myvideoimgnew);
                        }

                        if (file_exists($tothumbdir)) {
                            $query = "UPDATE audios SET ext='.jpg' WHERE AUDIOID='" . mysql_real_escape_string($audioid) . "'";
                            $conn->execute($query);
                        }
                    }
                }
            } else {
                $error = $lang['839'];
            }
        }
    }
} else {
    header("Location:$config[baseurl]/login.php");
    exit;
}

STemplate::assign('pagetitle', "$lang[797]");

//TEMPLATES BEGIN
STemplate::assign('error', $error);
STemplate::display('header.tpl');
STemplate::display('upload.tpl');
STemplate::display('footer.tpl');
//TEMPLATES END
?>